import {IpcChannel, IpcMainHandle} from "../ipc";

import {createLocalFile, loadLocalFile} from "../hook/dataFileHook";

export function useFileHandle(): Pick<
	IpcMainHandle,
	IpcChannel.FileRead | IpcChannel.FileWrite
> {
	return {
		[IpcChannel.FileRead]: (event, args) => {
			// console.log(args);
			let filename = args.filename;
			let data = loadLocalFile(filename);
			return {data: data};
		},
		[IpcChannel.FileWrite]: (event, args) => {
			// console.log(args);
			let filename = args.filename,
				data = args.data;
			return createLocalFile(filename, data)
				.then((r) => {
					return r;
				})
				.catch((err) => {
					// console.log(err);
					return false;
				});
		},
	};
}
